Thread: convertion string to char ch[]

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    convertion string to char ch[]

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string line = "Today! I'm very-tired "; // if i do like this, the system will hault and Windows error msg appear about the error
        char str[] = ""; // if i use char str[] = "Today! I'm very-tired " every thing is fine
        strcpy(str, line.c_str());
        char* new_word;
        int total=0;
    
        new_word = strtok(str, "-.,;:~!#%^&*()=+[]{}\\<>|?/\"\t\v\n\r\f ");
        
        while (new_word != NULL)
        {
           string temp = string(new_word);
           cout << temp << endl;
           new_word = strtok(NULL, "-.,;:~!#%^&*()=+[]{}\\<>|?/\"\t\v\n\r\f ");
        }
    }
    The problem is the exercise i use read from text file, so i used getline to get the string line. How can i use strtok( char * c, const char * ch)?
    And how to convert a string into char ch[] ?
    Thx and regard

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char str[] = "";
    Whilst valid, it does NOT have enough space to copy the string into.

    Also, try using a std::string approach to tokenizing rather than running back to messy strtok()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    30
    Also, try using a std::string approach to tokenizing rather than running back to messy strtok()
    Sorry, i'm new with c+ so i don't get it.

    I did try the size by string.size() and but into chr str[size] but it had error for initialize char str[]

    What should i do?
    Thx

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could allocate the char array dynamically, and you should note that you need size() + 1 char's - one for null terminator as well.

    But you can tokenize the string with find_first_of, find_first_not_of and substr too.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    30
    Thx for the tip, i did it. I know dynamic but not much attention on that.

    Code:
    int size = line.length();
        char *str;
        str = new char[size+1];
        strcpy(str, line.c_str());
    Thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What should i do?
    Keep reading more of the C++ std::string manual until you do "get it".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    30
    I have read some STL string. It's very helpful.

    Thank you guys alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM